/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.kn.cstf2.utils;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;

public class MailSender {

    public void sendEmail(ArrayList<String> to, String subject, String body, String archivo, String archivo2, boolean external) {

        ArrayList<InternetAddress> addTo = new ArrayList<InternetAddress>();

        //StringArrayToInternetAddress(to);
        Properties props = new Properties();

        props.put("mail.smtp.host", "10.29.68.53");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, null);
        String usuario = System.getProperty("user.name");

        if (external) {
            to.add("external." + usuario + "@kuehne-nagel.com");
        } else {
            to.add(usuario + "@kuehne-nagel.com");
        }

        try {
            for (String address : to) {
                if (address != null) {
                    System.out.println("Address:" + address);
                    System.out.println("Subject:" + subject);
                    System.out.println("Body:" + body);
                    addTo.add(new InternetAddress(address));
                }
            }
            InternetAddress[] addressTo = new InternetAddress[addTo.size()];
            addressTo = addTo.toArray(addressTo);
            InternetAddress[] addressFrom = new InternetAddress[1];
            if (external) {
                addressFrom[0] = new InternetAddress("external." + usuario + "@kuehne-nagel.com");
            } else {
                addressFrom[0] = new InternetAddress(usuario + "@kuehne-nagel.com");
            }
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("knmex.rfccl@kuehne-nagel.com"));
            message.setReplyTo(addressFrom);
            message.setRecipients(Message.RecipientType.TO, addressTo);
            message.setSubject(subject);

            MimeBodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setText(body.toString(), "utf-8");
//            messageBodyPart1.addHeader("Content-Type", "text/html; charset=UTF-8");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart1);

            MimeBodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2.setDataHandler(new DataHandler(new FileDataSource(archivo)));

            String substring = archivo.substring(archivo.lastIndexOf("/") + 1);

            messageBodyPart2.setFileName(substring);
            multipart.addBodyPart(messageBodyPart2);
            System.out.println("Archivo destino: " + archivo);
            System.out.println("Nombre: " + substring);

            MimeBodyPart messageBodyPart3 = new MimeBodyPart();
            messageBodyPart3.setDataHandler(new DataHandler(new FileDataSource(archivo2)));

            String substring2 = archivo2.substring(archivo2.lastIndexOf("/") + 1);

            messageBodyPart3.setFileName(substring2);
            multipart.addBodyPart(messageBodyPart3);
            System.out.println("Archivo destino: " + archivo2);
            System.out.println("Nombre: " + substring2);

            message.setContent(multipart);

            Transport t = session.getTransport("smtp");
            t.connect("knmex.rfccl", "ZafknIT2013");
            t.sendMessage(message, message.getAllRecipients());
            t.close();

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
    
    public void sendEmail(ArrayList<String> to, String subject, String body, String archivo, boolean external) {

        ArrayList<InternetAddress> addTo = new ArrayList<InternetAddress>();

        //StringArrayToInternetAddress(to);
        Properties props = new Properties();

        props.put("mail.smtp.host", "10.29.68.53");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, null);
        String usuario = System.getProperty("user.name");

        if (external) {
            to.add("external." + usuario + "@kuehne-nagel.com");
        } else {
            to.add(usuario + "@kuehne-nagel.com");
        }

        try {
            for (String address : to) {
                if (address != null) {
                    System.out.println("Address:" + address);
                    System.out.println("Subject:" + subject);
                    System.out.println("Body:" + body);
                    addTo.add(new InternetAddress(address));
                }
            }
            InternetAddress[] addressTo = new InternetAddress[addTo.size()];
            addressTo = addTo.toArray(addressTo);
            InternetAddress[] addressFrom = new InternetAddress[1];
            if (external) {
                addressFrom[0] = new InternetAddress("external." + usuario + "@kuehne-nagel.com");
            } else {
                addressFrom[0] = new InternetAddress(usuario + "@kuehne-nagel.com");
            }
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("knmex.rfccl@kuehne-nagel.com"));
            message.setReplyTo(addressFrom);
            message.setRecipients(Message.RecipientType.TO, addressTo);
            message.setSubject(subject);

            MimeBodyPart messageBodyPart1 = new MimeBodyPart();
            messageBodyPart1.setText(body.toString(), "utf-8");
//            messageBodyPart1.addHeader("Content-Type", "text/html; charset=UTF-8");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart1);

            MimeBodyPart messageBodyPart2 = new MimeBodyPart();
            messageBodyPart2.setDataHandler(new DataHandler(new FileDataSource(archivo)));

            String substring = archivo.substring(archivo.lastIndexOf("/") + 1);

            messageBodyPart2.setFileName(substring);
            multipart.addBodyPart(messageBodyPart2);
            System.out.println("Archivo destino: " + archivo);
            System.out.println("Nombre: " + substring);

            message.setContent(multipart);

            Transport t = session.getTransport("smtp");
            t.connect("knmex.rfccl", "ZafknIT2013");
            t.sendMessage(message, message.getAllRecipients());
            t.close();

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    public void sendEmail(ArrayList<String> to, String subject, String body, boolean external) {

        ArrayList<InternetAddress> addTo = new ArrayList<InternetAddress>();

        //StringArrayToInternetAddress(to);
        Properties props = new Properties();

        props.put("mail.smtp.host", "10.29.68.53");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, null);
        String usuario = System.getProperty("user.name");

        if (external) {
            to.add("external." + usuario + "@kuehne-nagel.com");
        } else {
            to.add(usuario + "@kuehne-nagel.com");
        }

        try {
            for (String address : to) {
                if (address != null) {
                    System.out.println("Address:" + address);
                    System.out.println("Subject:" + subject);
                    System.out.println("Body:" + body);
                    addTo.add(new InternetAddress(address));
                }
            }
            InternetAddress[] addressTo = new InternetAddress[addTo.size()];
            addressTo = addTo.toArray(addressTo);
            InternetAddress[] addressFrom = new InternetAddress[1];
            if (external) {
                addressFrom[0] = new InternetAddress("external." + usuario + "@kuehne-nagel.com");
            } else {
                addressFrom[0] = new InternetAddress(usuario + "@kuehne-nagel.com");
            }
//            
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("knmex.rfccl@kuehne-nagel.com"));
            message.setReplyTo(addressFrom);
            message.setRecipients(Message.RecipientType.TO, addressTo);
            message.setSubject(subject);
            message.setText(body.toString(), "utf-8");

//            message.setDataHandler(new DataHandler(new FileDataSource(filepath)));
//            message.setFileName("tempo.pdf");
//            log.info(filepath);
            Transport t = session.getTransport("smtp");
            t.connect("knmex.rfccl", "ZafknIT2013");
            t.sendMessage(message, message.getAllRecipients());
            t.close();

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
    
    
    public static void sendEmail(ArrayList<String> to, String subject, String body) {

        ArrayList<InternetAddress> addTo = new ArrayList<InternetAddress>();

        //StringArrayToInternetAddress(to);
        Properties props = new Properties();

        props.put("mail.smtp.host", "10.29.68.53");
        props.setProperty("mail.smtp.starttls.enable", "true");
        props.setProperty("mail.smtp.port", "25");
        props.setProperty("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, null);
        String usuario = System.getProperty("user.name");

   

        try {
            for (String address : to) {
                if (address != null) {
                    System.out.println("Address:" + address);
                    System.out.println("Subject:" + subject);
                    System.out.println("Body:" + body);
                    addTo.add(new InternetAddress(address));
                }
            }
            InternetAddress[] addressTo = new InternetAddress[addTo.size()];
            addressTo = addTo.toArray(addressTo);
            InternetAddress[] addressFrom = new InternetAddress[1];
         
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("knmex.rfccl@kuehne-nagel.com"));
            message.setRecipients(Message.RecipientType.TO, addressTo);
            message.setSubject(subject);
            message.setText(body.toString(), "utf-8");

            Transport t = session.getTransport("smtp");
            t.connect("knmex.rfccl", "ZafknIT2013");
            t.sendMessage(message, message.getAllRecipients());
            t.close();

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    private InternetAddress[] StringArrayToInternetAddress(String[] to) {
        InternetAddress[] addressTo = new InternetAddress[to.length];
        try {
            for (int i = 0; i < to.length; i++) {
                addressTo[i] = new InternetAddress(to[i]);
            }
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }

        return addressTo;
    }
}
